define(['common/breeze/inputconstants'], function(inputconstants) {

var keyboardinput = {};

var down = {};
var buttonEvents = {};

keyboardinput.init = function(clearInputPass) {
  clearInputPass.addCallback(onClearInputPass);
  window.addEventListener('keydown', onKeyDown, false);
  window.addEventListener('keyup', onKeyUp, false);
};

keyboardinput.down = function(keycode) {
  return !!down[keycode];
};

keyboardinput.lastFrameEvents = function(keycode) {
  return buttonEvents[keycode] || [];
};

function onClearInputPass(timeDeltaMillis) {
  buttonEvents = {};
};

function onKeyDown(ev) {
  if (!down[ev.keyCode]) {
    addEvent(ev.keyCode, inputconstants.ButtonEvent.PRESS);
  }
  down[ev.keyCode] = true;
};

function onKeyUp(ev) {
  if (down[ev.keyCode]) {
    addEvent(ev.keyCode, inputconstants.ButtonEvent.RELEASE);
  }
  delete down[ev.keyCode];
};

function addEvent(keyCode, buttonEvent) {
  if (buttonEvents[keyCode] == undefined) {
    buttonEvents[keyCode] = [];
  }
  buttonEvents[keyCode].push(buttonEvent);
};

return keyboardinput;

});
